package com.android.infantschool.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.infantschool.R;
import com.android.infantschool.bean.BaseBean;
import com.android.infantschool.bean.ParentsBindingBean;
import com.android.infantschool.bean.PatriarchBean;
import com.android.infantschool.model.config.AppConfig;
import com.android.infantschool.model.config.AppDataSource;
import com.android.infantschool.model.mvp.presenter.PatriarchPresenter;
import com.android.infantschool.model.mvp.presenter.PatriarchPresenterImpl;
import com.android.infantschool.model.mvp.view.PatriarchView;
import com.android.infantschool.ui.adapter.PatriarchAdapter;
import com.android.infantschool.util.ToastUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PatriarchActivity extends BaseActivity implements PatriarchAdapter.OnLister, OnRefreshListener, OnLoadMoreListener, PatriarchView {

    @BindView(R.id.img_back)
    public ImageView imgBack;
    @BindView(R.id.title)
    public RelativeLayout rlTitle;
    @BindView(R.id.tv_title)
    public TextView tvTitle;
    @BindView(R.id.recyclerView)
    public RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    public SmartRefreshLayout refreshLayout;

    public PatriarchAdapter adapter;
    private PatriarchPresenter patriarchPresenter = null;

    private int pageNum = 0;//总页数
    private int pageNow = 1;//当前请求的第几页
    private boolean isRefresh = false;//false:刷新  true:加载

    public static Intent newIntent(Activity activity) {
        Intent intent = new Intent(activity, PatriarchActivity.class);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_patriarch);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        patriarchPresenter = new PatriarchPresenterImpl(this);
        rlTitle.setPadding(0, this.height, 0, 0);
        tvTitle.setText("家长留言");

        adapter = new PatriarchAdapter(this, this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);

        isRefresh = false;
        Map<String, String> map = new HashMap<>();
        map.put("token", AppConfig.getToken());
        map.put("page", String.valueOf(pageNow));//第几页
        map.put("limit", "5");
        Log.e(TAG, "map:" + map.toString());
        patriarchPresenter.getPatriarch(map);
        this.showLoading();

        //设置 Footer 为 球脉冲 样式
        refreshLayout.setRefreshHeader(new ClassicsHeader(this));
        refreshLayout.setRefreshFooter(new ClassicsFooter(this));
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setOnLoadMoreListener(this);
    }

    @Override
    public void lister(int position) {
        startActivity(DetailsActivity.newIntent(this, "查看留言", adapter.list.get(position).getContent()));
    }

    @OnClick({R.id.img_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                this.finish();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        isRefresh = false;
        if (patriarchPresenter != null) {
            patriarchPresenter.onDestroy();
            patriarchPresenter = null;
        }
    }

    @Override
    public void onRefresh(@NonNull final RefreshLayout refreshLayout) {
        isRefresh = false;
        pageNow = 1;
        Map<String, String> map = new HashMap<>();
        map.put("token", AppConfig.getToken());
        map.put("page", String.valueOf(pageNow));//第几页
        map.put("limit", "5");
        Log.e(TAG, "map:" + map.toString());
        patriarchPresenter.getPatriarch(map);
    }

    @Override
    public void onLoadMore(@NonNull final RefreshLayout refreshLayout) {
        isRefresh = true;
        pageNow++;
        if (pageNow > pageNum) {//没有最新数据
            refreshLayout.finishLoadMore(true);
            refreshLayout.finishRefresh(true);
            return;
        }
        Map<String, String> map = new HashMap<>();
        map.put("token", AppConfig.getToken());
        map.put("page", String.valueOf(pageNow));//第几页
        map.put("limit", "5");
        Log.e(TAG, "map:" + map.toString());
        patriarchPresenter.getPatriarch(map);
    }

    @Override
    public void errorPatriarch(String error) {
        this.dismissLoading();
        refreshLayout.finishLoadMore(true);
        refreshLayout.finishRefresh(true);
        ToastUtil.showShortToast(this, "网络异常:" + error);
    }

    @Override
    public void successPatriarch(BaseBean baseBean) {
        this.dismissLoading();
        refreshLayout.finishLoadMore(true);
        refreshLayout.finishRefresh(true);
        PatriarchBean bean = (PatriarchBean) baseBean;
        if (bean.getCode() == 0) {
            pageNum = bean.getCount() / 5 + 1;//总条数
            if (isRefresh) {//加载
                adapter.addDataAll(bean.getData());
            } else {//刷新
                adapter.setData(bean.getData());
            }
            Log.e(TAG, "size:" + adapter.list.size());
        } else {
            ToastUtil.showShortToast(this, bean.getMsg());
        }
    }
}